// pages/list/evaluate/index.js
const $api = require('../../../../request/api.js').API;
import Toast from '../../../../miniprogram_npm/@vant/weapp/toast/toast';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    orderId: '',
    noTags: ['提前点送达', '态度不好', '送达超时', '送错产品', '仪表不整洁', '送达不通知'],
    tags: ['很准时', '热情礼貌', '仪表整洁', '货品完好'],
    waiterScore: 0, //5表示满意 1表示不满意
    isWrite: false, //如果是满意的是否写评价 true表示写，false表示不写
    waiterEvaluate: '', //服务员评价信息
    businessEvaluate: '', //商户评价信息
    businessScore: '', //商户评分
    checked: true,
    imgList: ['/static/waiter.png'],
    completeDate: '',
    waiterName: '',
    businessLogo: '',
    businessName: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.orderId) {
      console.log(options);
      this.setData({
        orderId: options.orderId
      });
      this.getData();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},
  async getData() {
    const res = await $api.orderDetail({ id: this.data.orderId });
    const { businessLogo, waiterName, completeDate, businessName } = res.data.data;
    this.setData({
      businessLogo,
      waiterName,
      completeDate,
      businessName
    });
  },
  // 选择图片
  chooseImg() {
    wx.chooseMedia({
      mediaType: 'image',
      sourceType: ['album', 'camera'],
      success: res => {
        let list = res.tempFiles.map(item => item.tempFilePath);
        this.setData({
          imgList: this.data.imgList.concat(list)
        });
      },
      fail: err => {}
    });
  },
  // 预览图片
  previewImg(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.item
    });
  },
  // 删除图片
  deleteImg(e) {
    let index = e.currentTarget.dataset.index;
    this.data.imgList.splice(index, 1);
    this.setData({
      imgList: this.data.imgList
    });
  },
  // 是否满意
  handleSatisfaction(e) {
    this.setData({
      waiterScore: e.currentTarget.dataset.type,
      isWrite: false,
      waiterEvaluate: ''
    });
  },
  // 满意的前提下是否写评价
  writeEvaluate() {
    this.setData({
      isWrite: !this.data.isWrite
    });
  },
  // 点击标签评价
  tagEvaluate(e) {
    const { val } = e.currentTarget.dataset;
    const { waiterEvaluate } = this.data;
    this.setData({
      waiterEvaluate: waiterEvaluate + ' ' + val
    });
  },
  evaluateInput(e) {
    const { type } = e.currentTarget.dataset;
    if (type == 1) {
      this.setData({
        waiterEvaluate: e.detail
      });
    } else {
      this.setData({
        businessEvaluate: e.detail
      });
    }
  },
  scoreChange(e) {
    this.setData({
      businessScore: e.detail
    });
  },
  submit() {
    const { waiterEvaluate, businessEvaluate, waiterScore, businessScore, orderId } = this.data;
    if (!waiterScore) {
      Toast.fail('请给服务员评价');
      return;
    }

    if (!businessScore) {
      Toast.fail('请给商户评分');
      return;
    }
    const params = {
      waiterEvaluate,
      businessEvaluate,
      waiterScore,
      businessScore,
      orderId
    };
    $api.evaluationOrder(params).then(() => {
      const pages = getCurrentPages().at(-2);
      // 判断上一页是哪个页面来的，如果是详情页面就需要调接口，如果是订单列表页面就需要改变目前订单对于评价的状态
      if (pages.route == 'pages/order/index') {
        const dataList = pages.data.dataList;
        const val = dataList.find(item => item.orderId == orderId);
        val.evaluateState = 1;
        if (val) {
          pages.setData({
            dataList
          });
        }
      } else if (pages.route == 'subcontract/order/pages/details/index') {
        pages.getData();
      }
      Toast.success({
        forbidClick: true,
        message: '评论成功'
      });
      setTimeout(() => {
        wx.navigateBack({
          delta: 1
        });
      }, 2000);
    });
  },
  // 存储
  storage() {
    wx.chooseImage({
      count: 0,
      success: res => {
        console.log(res);
        $api.uploadOss(res.tempFilePaths[0], 'test').then(res => {});
      }
    });

    // $api.uploadOss().then(res=>{

    // })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
});
